<?php
  require_once '../classes/ConnectionDB.php';
  require_once '../classes/UtenteManager.php';
  require_once '../classes/TestDataManager.php';
  require_once '../bean/Argomento.php';
  require_once '../classes/LoadProperties.php';

  $properties = new LoadProperties();

  $properties->initServices();

  $connection = ConnectionDB::getInstance()->connectToDB();
  $codiceUtente = $_COOKIE['codiceUtente'];
  $result = UtenteManager::getInstance()->getUtente($connection, $codiceUtente);

  $argomenti = TestDataManager::getInstance()->getArgomenti($connection, $codiceUtente);

  $codiceDomanda = $_GET["codiceDomanda"];

  $domanda = TestDataManager::getInstance()->getDomanda($connection, $codiceDomanda);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Dettaglio domanda</title>
      <script src="../glassboxLatest/glassbox.2.0.0/javascripts/prototype.js" type="text/javascript"></script>
      <script src="../glassboxLatest/glassbox.2.0.0/javascripts/scriptaculous/effects.js" type="text/javascript"></script>
      <script src="../glassboxLatest/glassbox.2.0.0/javascripts/glassbox/glassbox.js" type="text/javascript"></script>
      <style type="text/css">
        @import url("../css/style.css");
        #divVeroFalso,#divRispMultipla
        {
          padding-top: 5%;
        }
        #tableRes
        {
          padding-top: 8%;
        }
      </style>
      <script type="text/javascript">
        path_to_root_dir = "../glassboxLatest/glassbox.2.0.0/";
        var domanda = "";
        var rimanenti = 0;

        window.onload = function(){
          var header = new GlassBox();
          header.init( 'header', '590px', '250px', 'hidden','', true, false);
          header.ipos();
          header.appear();
          var bodyBox = new GlassBox();
          bodyBox.init( 'bodyBox', '920px', '520px', 'hidden','', true, false);
          bodyBox.ipos();
          bodyBox.appear();

          if(<?= $domanda->getTipologia() ?> == '1')
          {
            document.getElementById('divVeroFalso').style.display="block";
            document.getElementById('divRispMultipla').style.display="none";
          }
          else if(<?= $domanda->getTipologia() ?> == '2')
          {
            document.getElementById('divVeroFalso').style.display="none";
            document.getElementById('divRispMultipla').style.display="block";
          }

        }

        function loadDomanda()
        {
          var tipologia = document.getElementById('tipologia').value;
          if(tipologia==1)
          {
            document.getElementById('divVeroFalso').style.display="block";
            document.getElementById('divRispMultipla').style.display="none";
          }
          else if(tipologia==2)
          {
            document.getElementById('divVeroFalso').style.display="none";
            document.getElementById('divRispMultipla').style.display="block";
          }

        }

        function deleteDomanda()
        {
          var tipologia = document.getElementById('tipologia').value;
          document.formDelete.tipologia.value = tipologia;
          document.formDelete.submit();
        }

        function modifica()
        {
          var codiceDomanda = <?= $domanda->getCodiceDomanda() ?>;
          var codiceArgomento = document.getElementById('codiceArgomento').value;
          var tipologia = document.getElementById('tipologia').value;

          if(tipologia == '1')
          {
            var strError="";
            var punteggio = document.getElementById('punteggioVF').value;
            var domanda = document.getElementById('domandaVF').value;
            if(domanda.length==0)
            {
              strError +="10*";
            }
            var rispCorretta="";
            if(document.getElementById('vero').checked)
              rispCorretta = "V";
            if(document.getElementById('falso').checked)
              rispCorretta = "F";
            if(rispCorretta=="")
            {
              strError +="11*";
            }


            if(strError.length>0)
            {
              location.href='../portal/manageErrors.php?lista='+strError;
            }
            else
            {
              document.formVF.codiceDomanda = codiceDomanda;
              document.formVF.codiceArgomento.value = codiceArgomento;
              document.formVF.tipologia.value = tipologia;
              document.formVF.punteggio.value = punteggio;
              document.formVF.domanda.value = domanda;
              document.formVF.rispCorretta.value = rispCorretta;

              document.formVF.submit();
            }

          }

          if(tipologia == '2')
          {
            var strError="";
            var punteggio = document.getElementById('punteggioRM').value;
            var domanda = document.getElementById('domandaRM').value;
            if(domanda.length==0)
            {
              strError +="10*";
            }
            if(!document.getElementById('1alternativaCorretta').checked && !document.getElementById('2alternativaCorretta').checked && !document.getElementById('3alternativaCorretta').checked && !document.getElementById('4alternativaCorretta').checked)
            {
              strError += "11*";
            }
            document.formRisMultipla.codiceDomanda = codiceDomanda;
            document.formRisMultipla.codiceArgomento.value = codiceArgomento;
            document.formRisMultipla.tipologia.value = tipologia;
            document.formRisMultipla.punteggio.value = punteggio;
            document.formRisMultipla.domanda.value = domanda;
            document.formRisMultipla.primaAlternativa.value = document.getElementById('primaAlternativa').value;
            document.formRisMultipla.secondaAlternativa.value = document.getElementById('secondaAlternativa').value;
            document.formRisMultipla.terzaAlternativa.value = document.getElementById('terzaAlternativa').value;
            document.formRisMultipla.quartaAlternativa.value = document.getElementById('quartaAlternativa').value;
            if(document.formRisMultipla.primaAlternativa.value=="")
              strError +="12*";
            if(document.formRisMultipla.secondaAlternativa.value=="")
              strError +="13*";
            if(document.formRisMultipla.terzaAlternativa.value=="")
              strError +="14*";
            if(document.formRisMultipla.quartaAlternativa.value=="")
              strError +="15*";
            if(strError.length>0)
            {
              location.href='../portal/manageErrors.php?lista='+strError;
            }
            else
            {
              if(document.getElementById('1alternativaCorretta').checked)
                document.formRisMultipla.alternativaCorretta.value='1';
              if(document.getElementById('2alternativaCorretta').checked)
                document.formRisMultipla.alternativaCorretta.value='2';
              if(document.getElementById('3alternativaCorretta').checked)
                document.formRisMultipla.alternativaCorretta.value='3';
              if(document.getElementById('4alternativaCorretta').checked)
                document.formRisMultipla.alternativaCorretta.value='4';
              document.formRisMultipla.submit();
            }

          }

        }

        function checkLenDomanda(domanda)
        {
          var rest;
          max=1024;
          var len = document.getElementById(domanda).value.length;

          if (len > max) {
            document.getElementById(domanda).value = document.getElementById(domanda).value.substring(0,max)
            rest = 0
          }
          else {
            rest = max - len
          }

          document.getElementById('numCaratteriRimanenti').innerHTML = rest;

        }


      </script>
    </head>
    <body>
      <form name="formDelete" method="post" action="../testManaging/newQuestionListener.php">
        <input type="hidden" name="operation" value="delete"/>
        <input type="hidden" name="tipologia" />
        <input type="hidden" name="codiceDomanda" value="<?= $domanda->getCodiceDomanda() ?>" />˙
      </form>
      <form name="formVF" method="post" action="../testManaging/newQuestionListener.php">
        <input type="hidden" name="operation" value="update"/>
        <input type="hidden" name="codiceDomanda" value="<?= $domanda->getCodiceDomanda() ?>" />
        <input type="hidden" name="codiceArgomento" />
        <input type="hidden" name="tipologia" />
        <input type="hidden" name="punteggio" />
        <input type="hidden" name="domanda" />
        <input type="hidden" name="rispCorretta" />
      </form>
      <form name="formRisMultipla" method="post" action="../testManaging/newQuestionListener.php">
        <input type="hidden" name="operation" value="update"/>
        <input type="hidden" name="codiceDomanda" value="<?= $domanda->getCodiceDomanda() ?>" />
        <input type="hidden" name="codiceArgomento" />
        <input type="hidden" name="tipologia" />
        <input type="hidden" name="punteggio" />
        <input type="hidden" name="domanda" />
        <input type="hidden" name="primaAlternativa" />
        <input type="hidden" name="secondaAlternativa" />
        <input type="hidden" name="terzaAlternativa" />
        <input type="hidden" name="quartaAlternativa" />
        <input type="hidden" name="alternativaCorretta" />
      </form>
    <?php include '../portal/header.php'; ?>
      <center>
        <div id="header">
          <center>
            <table cellpadding="7">

              <tr>
                <td>Argomento : </td>
                <td>
                  <select name="codiceArgomento" id="codiceArgomento">
                  <?
                    for ($j = 0; $j < count($argomenti); $j++)
                    {
                  ?>
                      <option value="<?= $argomenti[$j]->getCodice() ?>"
                  <? if ($argomenti[$j]->getCodice() == $domanda->getCodiceArgomento())
                        echo 'selected' ?>>
                    <?= $argomenti[$j]->getNome() ?></option>
                  <? } ?>
                    </select>
                  </td>
                </tr>
                <tr>
                  <td>Tipologia : </td>
                  <td>
                    <select name="tipologia" id="tipologia" onchange="javascript:loadDomanda();">
                      <option value="1" <? if ($domanda->getTipologia() == '1')
                        echo 'selected' ?>>Vero / Falso</option>
                  <option value="2" <? if ($domanda->getTipologia() == '2')
                          echo 'selected' ?>>Risposta multipla</option>
                </select>
              </td>
            </tr>

            <?
                          if ($domanda->getLink() != "")
                          {
                            $tmp = explode("/", $domanda->getLink());
                            $nomefileStored = $tmp[count($tmp) - 1];
            ?>
                            <tr>
                              <th>File allegato : </th>
                              <td><a href="<?= $domanda->getLink() ?>"><?= $nomefileStored ?></a></td>
                            </tr>
                            <tr>
                              <td>
                                <form method="POST" action="deleteFileByFTP.php">
                                  <input type="hidden" name="code" value="<?= $domanda->getCodiceDomanda() ?>" />
                                  <input type="hidden" name="path" value="<?= $domanda->getLink() ?>" />
                                  <input type="hidden" name="nomeFile"value="<?= $nomefileStored ?>" />
                                  <input type="submit" value="Elimina allegato" />
                                </form>
                              </td>
                            </tr>

            <?
                          }
                          else
                          {
            ?>
                            <tr>
                              <th>Allega un file</th>
                              <td>
                                <form enctype="multipart/form-data" action="../testManaging/uploader.php" method="POST">
                                  <input type="hidden" name="code" value="<?= $domanda->getCodiceDomanda() ?>" />
                                  <input type="hidden" name="operation" value="updateAttachment" />
                                  <input name="fileTmp" type="file"  />
                                  <input type="submit" value="Upload File" />
                                </form>
                              </td>
                            </tr>                
<? } ?>

                        </table>
                      </center>

                    </div>
                  </center>
                  <center>
                    <script>
                      domanda = '<?= $domanda->getTestoDomanda() ?>';
                      domanda = domanda.replace(/\\'/gi, "'");
                      rimanenti = 1024-domanda.length;
                    </script>
                    <h3><div style="color: white"><b>Caratteri rimanenti :</b></div>&nbsp;<div style="color: white" id="numCaratteriRimanenti"><script>document.write(rimanenti);</script></div></h3>

                    <div id="bodyBox">
                      <div id="divVeroFalso" style="display: none" >
                        <table width="100%">
                          <tr>

                            <th>Punteggio : </th>

                            <td><select name="punteggioVF" id="punteggioVF">
                                <option value="1" <? if ($domanda->getPunteggio() == '1')
                            echo 'selected' ?>>1</option>
                    <option value="2" <? if ($domanda->getPunteggio() == '2')
                              echo 'selected' ?>>2</option>
                      <option value="3" <? if ($domanda->getPunteggio() == '3')
                                echo 'selected' ?>>3</option>
                        <option value="4" <? if ($domanda->getPunteggio() == '4')
                                  echo 'selected' ?>>4</option>
                          <option value="5" <? if ($domanda->getPunteggio() == '5')
                                    echo 'selected' ?>>5</option>
                            <option value="6" <? if ($domanda->getPunteggio() == '6')
                                      echo 'selected' ?>>6</option>
                              <option value="7" <? if ($domanda->getPunteggio() == '7')
                                        echo 'selected' ?>>7</option>
                                <option value="8" <? if ($domanda->getPunteggio() == '8')
                                          echo 'selected' ?>>8</option>
                                  <option value="9" <? if ($domanda->getPunteggio() == '9')
                                            echo 'selected' ?>>9</option>
                                    <option value="10" <? if ($domanda->getPunteggio() == '10')
                                              echo 'selected' ?>>10</option>

                                    </select>
                                  </td>


                                </tr>
                                <tr>
                                  <th>Domanda : </th>
                                  <td>
                                    <textarea onkeyup="javascript:checkLenDomanda('domandaVF');" cols="90" rows="8" name="domandaVF" id="domandaVF" ><?= $domanda->getTestoDomanda() ?></textarea>
                                  </td>
                                </tr>


                              </table>
                              <br/><br/><br/>
                              <table width="100%">
                                <tr>
                                  <th>Risposta corretta : </th>

                                </tr>
                                <tr>

                                <table>

                                  <tr><td>Vero</td><td><input type="checkbox" name="vero" id="vero" <? if ($domanda->getRisposta() == 'V')
                                                echo 'checked' ?> onclick="document.getElementById('falso').checked=false;"/></td></tr>
                   <tr><td>Falso</td><td><input type="checkbox" name="falso" id="falso" <? if ($domanda->getRisposta() == 'F')
                                                  echo 'checked' ?> onclick="document.getElementById('vero').checked=false;"/></td></tr>


                   </table>

                   </tr>



                 </table>
                 <input type="button" value="Modifica domanda" onclick="javascript:modifica()"/>
                 <input type="button" value="Elimina" onclick="javascript:deleteDomanda()"/>

               </div>
               <div id="divRispMultipla" style="display: none">
                 <table width="100%">
                   <tr>

                     <th>Punteggio : </th>

                     <td><select name="punteggioRM" id="punteggioRM">
                         <option value="1" <? if ($domanda->getPunteggio() == '1')
                                                    echo 'selected' ?>>1</option>
                                            <option value="2" <? if ($domanda->getPunteggio() == '2')
                                                      echo 'selected' ?>>2</option>
                                              <option value="3" <? if ($domanda->getPunteggio() == '3')
                                                        echo 'selected' ?>>3</option>
                                                <option value="4" <? if ($domanda->getPunteggio() == '4')
                                                          echo 'selected' ?>>4</option>
                                                  <option value="5" <? if ($domanda->getPunteggio() == '5')
                                                            echo 'selected' ?>>5</option>
                                                    <option value="6" <? if ($domanda->getPunteggio() == '6')
                                                              echo 'selected' ?>>6</option>
                                                      <option value="7" <? if ($domanda->getPunteggio() == '7')
                                                                echo 'selected' ?>>7</option>
                                                        <option value="8" <? if ($domanda->getPunteggio() == '8')
                                                                  echo 'selected' ?>>8</option>
                                                          <option value="9" <? if ($domanda->getPunteggio() == '9')
                                                                    echo 'selected' ?>>9</option>
                                                            <option value="10" <? if ($domanda->getPunteggio() == '10')
                                                                      echo 'selected' ?>>10</option>


                                                            </select>
                                                          </td>

                                                        </tr>
                                                        <tr>
                                                          <th>Domanda : </th>
                                                          <td>
                                                            <textarea onkeyup="javascript:checkLenDomanda('domandaRM');" cols="90" rows="8" name="domandaRM" id="domandaRM" ><?= $domanda->getTestoDomanda() ?></textarea>
                                                          </td>
                                                        </tr>

                                                      </table>
                                                      <br/><br/><br/>
                                                      <table>
                                                        <tr>
                                                          <th>1° Alternativa</th>
                                                          <td><input type="text" id="primaAlternativa" name="primaAlternativa" size="50" value="<?= $domanda->getRisposta1() ?>" /></td>
                                                          <td>Corretta: <input type="checkbox" id="1alternativaCorretta" name="alternativaCorretta"
                                                                               onclick="document.getElementById('2alternativaCorretta').checked=false;
                                                                                 document.getElementById('3alternativaCorretta').checked=false;
                                                                                 document.getElementById('4alternativaCorretta').checked=false;"
                                                                               value="1" <? if ($domanda->getRispostaEsatta() == '1')
                                                                        echo 'checked' ?>/></td>
                                                 </tr>
                                                 <tr>
                                                   <th>2° Alternativa</th>
                                                   <td><input type="text" id="secondaAlternativa" name="secondaAlternativa" size="50" value="<?= $domanda->getRisposta2() ?>"/></td>
                                                   <td>Corretta: <input type="checkbox" id="2alternativaCorretta" name="alternativaCorretta"
                                                                        onclick="document.getElementById('1alternativaCorretta').checked=false;
                                                                          document.getElementById('3alternativaCorretta').checked=false;
                                                                          document.getElementById('4alternativaCorretta').checked=false;"
                                                                        value="2" <? if ($domanda->getRispostaEsatta() == '2')
                                                                          echo 'checked' ?> /></td>
                                                   </tr>
                                                   <tr>
                                                     <th>3° Alternativa</th>
                                                     <td><input type="text" id="terzaAlternativa" name="terzaAlternativa" size="50" value="<?= $domanda->getRisposta3() ?>"/></td>
                                                     <td>Corretta: <input type="checkbox" id="3alternativaCorretta" name="alternativaCorretta"
                                                                          onclick="document.getElementById('1alternativaCorretta').checked=false;
                                                                            document.getElementById('2alternativaCorretta').checked=false;
                                                                            document.getElementById('4alternativaCorretta').checked=false;"
                                                                          value="3" <? if ($domanda->getRispostaEsatta() == '3')
                                                                            echo 'checked' ?>
                                                                            /></td>
                                                     </tr>
                                                     <tr>
                                                       <th>4° Alternativa</th>
                                                       <td><input type="text" id="quartaAlternativa" name="quartaAlternativa" size="50" value="<?= $domanda->getRisposta4() ?>"/></td>
                                                       <td>Corretta: <input type="checkbox" id="4alternativaCorretta" name="alternativaCorretta"
                                                                            onclick="document.getElementById('1alternativaCorretta').checked=false;
                                                                              document.getElementById('2alternativaCorretta').checked=false;
                                                                              document.getElementById('3alternativaCorretta').checked=false;"
                                                                            value="4" <? if ($domanda->getRispostaEsatta() == '4')
                                                                              echo 'checked' ?> /></td>
            </tr>


          </table>
          <input type="button" value="Modifica domanda" onclick="javascript:modifica()"/>
          <input type="button" value="Elimina" onclick="javascript:deleteDomanda()"/>
        </div>

      </div>
    </center>

  </body>
</html>
